package com.sh.data.engine.domain.shims.sql.builder;

import com.sh.data.engine.domain.base.model.enums.Database;
import com.sh.data.engine.domain.shims.sql.Context;
import com.sh.data.engine.domain.shims.sql.ddl.AddTableFieldParam;
import com.sh.data.engine.domain.shims.sql.ddl.CreateTableParam;
import com.sh.data.engine.domain.shims.sql.ddl.DDL;
import com.sh.data.engine.domain.shims.sql.ddl.EditTableFieldParam;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.io.Closeable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/5 15:16
 */
@Slf4j
public class DDLBuilder implements Closeable {

    private final Context context;

    public DDLBuilder(Database database, DataSource dataSource) throws SQLException {
        this(database, dataSource.getConnection());
    }

    public DDLBuilder(Database database, Connection connection) {
        this.context = new Context(database, connection);
    }

    public DDLBuilder reset() {
        this.context.reset();
        return this;
    }

    public DDL create(CreateTableParam param) {
        return DDL.create(context, param);
    }

    public DDL create(String sql) {
        return new DDL(context, sql);
    }

    public DDL alterTableName(String schema, String tableName, String newTableName) {
        return DDL.alterTableName(context, schema, tableName, newTableName);
    }

    public DDL alterTableComment(String schema, String tableName, String comment) {
        return DDL.alterTableComment(context, schema, tableName, comment);
    }

    public DDL alterViewComment(String schema, String tableName, String comment) {
        return DDL.alterViewComment(context, schema, tableName, comment);
    }

    public DDL addTableField(String schema, String tableName, List<AddTableFieldParam> paramList) {
        return DDL.addTableField(context, schema, tableName, paramList);
    }

    public DDL editTableField(String schema, String tableName, List<EditTableFieldParam> paramList) {
        return DDL.editTableField(context, schema, tableName, paramList);
    }

    public DDL deleteTableField(String schema, String tableName, List<String> fieldList) {
        return DDL.deleteTableField(context, schema, tableName, fieldList);
    }

    public DDL replaceTableFields(String schema, String tableName, List<AddTableFieldParam> fieldLists) {
        return DDL.replaceTableFields(context, schema, tableName, fieldLists);
    }

    public DDL dropTable(String schema, String tableName) {
        return DDL.dropTable(context, schema, tableName);
    }

    public DDL getTableField(String schema, String tableName) {
        return DDL.getTableField(context, schema, tableName);
    }

    @Override
    public void close() {
        try {
            this.context.reset();
            this.context.colseConnection();
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public String toString() {
        return context.toString();
    }
}
